import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import request from 'supertest';
import { AppModule } from './../src/app.module';

describe('AppController (e2e)', () => {
    let app: INestApplication;

    beforeEach(async () => {
        const moduleFixture: TestingModule = await Test.createTestingModule({
            imports: [AppModule],
        }).compile();

        app = moduleFixture.createNestApplication();
        await app.init();
    });

    afterAll(async () => {
        await app.close();
    });

    it('/ (GET)', () => {
        return request(app.getHttpServer()).get('/api').expect(404);
    });

    it('/auth/login (POST) - 无效凭据', () => {
        return request(app.getHttpServer())
            .post('/api/auth/login')
            .send({
                email: 'invalid@example.com',
                password: 'wrongpassword',
            })
            .expect(401);
    });

    it('/users (POST) - 创建用户', () => {
        return request(app.getHttpServer())
            .post('/api/users')
            .send({
                email: 'test@example.com',
                username: 'testuser',
                password: 'password123',
            })
            .expect(201)
            .expect((res) => {
                expect(res.body).toHaveProperty('id');
                expect(res.body.email).toBe('test@example.com');
                expect(res.body.username).toBe('testuser');
                expect(res.body).not.toHaveProperty('password');
            });
    });
});
